package com.template.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.template.entity.PostComment;
import com.template.result.PageBean;
import com.template.result.Result;
import com.template.service.CommunityService;
import com.template.vo.PostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RestController
@RequestMapping("/community")
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    @GetMapping("/getPostInfo")
    public Result getPostInfo(@RequestParam Integer postId,@RequestParam Integer userId) throws InvocationTargetException, IllegalAccessException {
//        if(userId==null){
//            throw new RuntimeException("未实现");
//            return Result.error("功能未实现");
//        }else{
        PostVO postInfo=communityService.getPostInfo(postId,userId);
        return Result.success(postInfo);

//        return null;
    }

    @GetMapping("/getPostList")
    public Result getPost(@RequestParam Integer pageNo,@RequestParam Integer pageSize,@RequestParam Integer userId) throws JsonProcessingException {
//        if(userId==null){
////            throw new RuntimeException("未实现");
//            return Result.error("功能未实现");
//        }else{
            PageBean pageBean =communityService.getPostList(pageNo,pageSize,userId);
            return Result.success(pageBean);

//        return null;
    }

    @GetMapping("/searchPosts")
    public Result searchPosts(@RequestParam String keyword,
                              @RequestParam Integer pageNo,
                              @RequestParam Integer pageSize,
                              @RequestParam Integer userId) throws JsonProcessingException {
        PageBean pageBean = communityService.searchPosts(keyword, pageNo, pageSize, userId);
        return Result.success(pageBean);
    }

    @PostMapping("/addPost")
    public Result addPost(@RequestBody PostVO postVO) throws JsonProcessingException {
//        System.out.println(postVO);
        communityService.addPost(postVO);
        return Result.success();
    }

    @PutMapping("/isGood")
    public Result isGood(@RequestParam Integer userId,@RequestParam Integer id){
        communityService.isGood(userId,id);
        return Result.success(200);
    }

    @PutMapping("/isNotGood")
    public Result isNotGood(@RequestParam Integer userId,@RequestParam Integer id){
        communityService.isNotGood(userId,id);
        return Result.success(200);
    }

    @PostMapping("/addPostComment")
    public Result addPostComment(@RequestBody PostComment postComment){
        communityService.addPostComment(postComment);
        return Result.success();
    }

    @GetMapping("/getPostCommentList")
    public Result getPostCommentList(@RequestParam Integer postId){
        List<PostComment> postComment =communityService.getPostCommentList(postId);
        return Result.success( postComment);
    }
}
























